package fi.hell.lunchit

import fi.hell.util.CookieService

class VoteController {

    static allowedMethods = [update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [voteInstanceList: Vote.list(params), voteInstanceTotal: Vote.count()]
    }

    def create = {
    	Vote voteInstance = new Vote()
        voteInstance.properties = params
        return [voteInstance: voteInstance]
    }

    def save = {
        Round round = Round.currentRound()
        Restaurant restaurant = Restaurant.get(params.id)
        params.vote = Round.currentVote(round)
        
        if (round) {
            params['round.id'] = round.id
        } else {
            flash.message = "${message(code:'unauthorized', default:'Your not allowed to do this operation when not logged in.')}"
        }
    	
        Vote voteInstance = new Vote(params)
        voteInstance.restaurant = restaurant
        if (voteInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'vote.label', default: 'Vote'), voteInstance.id])}"
        } else if (params.vote == 0) {
        	flash.message = "${message(code:'vote.votesAlreadyUsed', default:'You\'ve already used all your votes.')}"
        } else {
        	flash.message = "${message(code:'vote.saveFailed', default:'Failed to save your vote. Please try again or contact Mr. Hell if your problem persists!')}"
            log.error(voteInstance.errors)
        }
        redirect(url:grailsApplication.config.grails.serverURL)
    }

    def show = {
    	Vote voteInstance = Vote.get(params.id)
        if (!voteInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
            redirect(action: "list")
        }
        else {
            [voteInstance: voteInstance]
        }
    }

    def edit = {
    	Vote voteInstance = Vote.get(params.id)
        if (!voteInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [voteInstance: voteInstance]
        }
    }

    def update = {
    	Vote voteInstance = Vote.get(params.id)
        if (voteInstance) {
            if (params.version) {
                long version = params.version.toLong()
                if (voteInstance.version > version) {
                    
                    voteInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'vote.label', default: 'Vote')] as Object[], "Another user has updated this Vote while you were editing")
                    render(view: "edit", model: [voteInstance: voteInstance])
                    return
                }
            }
            voteInstance.properties = params
            if (!voteInstance.hasErrors() && voteInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'vote.label', default: 'Vote'), voteInstance.id])}"
                redirect(action: "show", id: voteInstance.id)
            }
            else {
                render(view: "edit", model: [voteInstance: voteInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
    	Vote voteInstance = Vote.get(params.id)
        if (voteInstance) {
            try {
                voteInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
        }
    	
    	redirect(url:grailsApplication.config.grails.serverURL)
    }
    
    def finalChoise = {
    	Round currentRound = Round.currentRound()
    	Restaurant choosedRestaurant = Restaurant.get(params.id)
    	currentRound.selection = choosedRestaurant
    	currentRound.save(flush:true)
    	redirect(url:grailsApplication.config.grails.serverURL)
    }
}
